package com.jxnu.duewang.zmly;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.jxnu.duewang.zmly.zmly.agency.IntroduceService;
import com.jxnu.duewang.zmly.zmly.model.IntroduceData;
import com.jxnu.duewang.zmly.zmly.model.IntroduceInfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.R.id.edit;
import static android.content.Context.MODE_PRIVATE;

public class StartActivity extends AppCompatActivity {

    private ArrayList<String> mUrls;
    private SharedPreferences mZmly;
    private int mInternetRequestCode=998;
    private int mNteworkStateRequestCode=999;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        applyPermission();
        mUrls=new ArrayList<>();
        mZmly = getSharedPreferences("zmly", MODE_PRIVATE);
        boolean pass = mZmly.getBoolean("rememberpass", true);
        if (pass) {
            requestNetworkMessage();
        }else {
            startThread(MainActivity.class,2000);
        }

    }

    private void startThread(final Class cla, final int time){
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(StartActivity.this,cla));
                overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
                try {
                    Thread.sleep(1200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finish();
            }
        },time);
    }

    private void requestNetworkMessage(){
        IntroduceService introduceService = new Retrofit.Builder()
                .baseUrl("http://adse.ximalaya.com/ting/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(IntroduceService.class);
        Call<IntroduceInfo> introduceInfoCall = introduceService.getIntroduceInfo();
        introduceInfoCall.enqueue(new Callback<IntroduceInfo>() {
            @Override
            public void onResponse(Call<IntroduceInfo> call, Response<IntroduceInfo> response) {
                if (response.isSuccessful()) {
                    IntroduceInfo info = response.body();
                    List<IntroduceData> introduceDatas = info.getIntroduceDatas();
                    for (IntroduceData introduceData :
                            introduceDatas) {
                        mUrls.add(introduceData.getCover());
                    }
                    Intent intent = new Intent(StartActivity.this,IntroduceActivity.class);
                    intent.putStringArrayListExtra("urlList",mUrls);
                    final Intent intentThread = new Intent(intent);
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            startActivity(intentThread);
                            overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
                            try {
                                Thread.sleep(1200);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            finish();
                        }
                    },1000);
                }
            }

            @Override
            public void onFailure(Call<IntroduceInfo> call, Throwable t) {
                Toast.makeText(StartActivity.this, "网络请求异常", Toast.LENGTH_SHORT).show();
                startThread(MainActivity.class,2000);
            }
        });
    }

    public void applyPermission(){
        int internet = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
        if (internet== PackageManager.PERMISSION_DENIED) {
            mInternetRequestCode = 998;
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, mInternetRequestCode);
        }
        int networkState = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE);
        if (networkState== PackageManager.PERMISSION_DENIED) {
            mNteworkStateRequestCode = 999;
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_NETWORK_STATE}, mNteworkStateRequestCode);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

}
